/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */

package info.peigen.hotpot.business.customer.core.service.processor.partner;

import info.peigen.hotpot.business.customer.core.domain.partner.Partner;
import info.peigen.hotpot.business.customer.core.domain.partner.action.PartnerDomainAction;
import info.peigen.hotpot.business.customer.core.service.processor.base.AbstractCustomerServiceProcessor;
import info.peigen.hotpot.business.customer.facade.order.partner.PartnerApplyPermissionOrder;
import info.peigen.hotpot.business.customer.facade.order.partner.PartnerCreateOrder;
import info.peigen.hotpot.common.service.base.ServiceOrderBase;
import info.peigen.hotpot.common.service.base.processor.Processor;
import info.peigen.hotpot.common.service.base.processor.ProcessorServiceContext;
import org.noear.solon.annotation.Component;

/**
 * <b>(PartnerApplyPermissionProcessor)</b>
 *
 * @author Lihai
 * @version 1.0.0
 * @since 2022/5/26
 */
@Component
public class PartnerApplyPermissionProcessor extends AbstractCustomerServiceProcessor implements Processor {
    @Override
    public Integer level() {
        return 54;
    }

    @Override
    public void execute(ProcessorServiceContext serviceContext) {
        Partner                     partner = (Partner) serviceContext.getDomain();
        PartnerApplyPermissionOrder order   = getOrder(serviceContext);
        PartnerDomainAction.mergePermission(partner, order);
    }

    public PartnerApplyPermissionOrder getOrder(ProcessorServiceContext serviceContext) {
        ServiceOrderBase order = serviceContext.getOrder();
        if (order instanceof PartnerApplyPermissionOrder) {
            return (PartnerApplyPermissionOrder) serviceContext.getOrder();
        } else if (order instanceof PartnerCreateOrder) {
            return PartnerApplyPermissionOrder.builder().build();
        }
        return null;
    }
}
